package com.example.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.domain.AreaEntity;
import com.example.domain.ProvinceEntity;
import com.example.domain.XsWQEntity;
import com.example.mapper.ProvinceMapper;
import com.example.mapper.XsWQMapper;
import com.example.service.AreaService;
import com.example.service.XsWQService;
import com.example.utils.config.CommonConfig;
import com.example.utils.poi.AddressParseUtils;

import lombok.extern.slf4j.Slf4j;

@Service
@Transactional
@Slf4j
public class XsWQServiceImp implements XsWQService {

	@Autowired
	XsWQMapper xsWQMapper;
	@Autowired
	AreaService areaService;
	@Autowired
	ProvinceMapper provinceMapper;

	@Override
	public List<XsWQEntity> queryXsWQData(String beginDate, String endDate) {
		String deptNamesStr = CommonConfig.getDeptNames();
		log.info("配置文件配置部门信息:{}", deptNamesStr);
		String[] deptNames = deptNamesStr.split(",");
		log.info("收到参数beginDate:{},endDate:{}", beginDate, endDate);
		List<XsWQEntity> wqList = xsWQMapper.queryXsWQData(deptNames, beginDate, endDate);
//		log.info("wqlist:{}",wqList);

		for (XsWQEntity wq : wqList) {
			String province = "";
			String city = "";
//			wq.setMsg(deptNamesStr);
			List<Map<String, String>> list = AddressParseUtils.addressResolution(wq.getAddress());
			String address = list.get(0).get("address");
			if (list.size() > 1) {
				Map<String, String> map = list.get(1);
				province = map.get("province");
				city = map.get("city");
				if (province.length() > 10) { // 直接匹配到省份，但是省份不对
					QueryWrapper<ProvinceEntity> wrapper = new QueryWrapper<ProvinceEntity>();
					wrapper.like("city", city);
					ProvinceEntity provinceEntity = provinceMapper.selectOne(wrapper);
					province = provinceEntity.getProvince();
				}
//				System.out.println(address+"--通过正则表达式匹配到--"+" 省:"+province+"  市:"+city);
			} else {
				List<AreaEntity> pList = areaService.queryProvinceData(wq.getLongitude(), wq.getLatitude());
				List<AreaEntity> cList = areaService.queryCityData(wq.getLongitude(), wq.getLatitude());
				if (pList.size() == 0) {
//					System.out.println(address+"-===-"+"未匹配到省份");
				} else if (pList.size() == 1) {
					province = pList.get(0).getName();
				} else {
					for (AreaEntity p : pList) {
						if (address.contains(p.getName())) {
							province = p.getName();
						}
					}
				}

				if (cList.size() == 0) {
//					System.out.println(address+"-===-"+"未匹配到市/区");
				} else if (cList.size() == 1) {
					city = cList.get(0).getName();
				} else {
					for (AreaEntity c : cList) {
						if (address.contains(c.getName())) {
							city = c.getName();
						}
					}
				}
				if ("".equals(city)) {
					float max = 0;
//					QueryWrapper<ProvinceEntity> wrapper = new QueryWrapper<ProvinceEntity>();
//					List<ProvinceEntity> provinceList = provinceMapper.selectList(wrapper);
//					for (ProvinceEntity p : provinceList) {
//						String str = p.getCity().replaceAll("\\[", "").replaceAll("\\]", "");
//						String[] citys = str.split(",");
//					}
					for (AreaEntity c : cList) { // 没有匹配到，进行模糊匹配,将匹配度最高的赋值
						float i = AddressParseUtils.Levenshtein(c.getName(), address);
						System.out.println(address + ",," + c.getName() + ",,," + i);
						if (i >= max) {
							max = i;
							city = c.getName();
						}
					}
				}
				if ("".equals(city)) {
					if (province.contains("市")) {
						city = province;
					} else {
						log.info(address + "  未匹配到city:" + city + ",, province:" + province);
					}
				} else {
					if ("".equals(province)) {
						QueryWrapper<ProvinceEntity> wrapper = new QueryWrapper<ProvinceEntity>();
						wrapper.like("city", city);
//						ProvinceEntity provinceEntity = provinceMapper.selectOne(wrapper);
						List<ProvinceEntity> provinceEntityList = provinceMapper.selectList(wrapper);

						if (provinceEntityList.size() > 0) {
							province = provinceEntityList.get(0).getProvince();
						}

					}
				}
//				System.out.println(address+"--=--"+" 省:"+province+"  市:"+city);
			}
			log.info(wq.getLastname() + ",地址:【" + wq.getAddress() + "】匹配后省份:" + province + ",市:" + city);
			wq.setProvince(province);
			wq.setCity(city);
		}
		return wqList;
	}

}
